package com.bwie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.ProdInfoMapper;
import com.bwie.pojo.ProdInfo;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Prod;
import com.bwie.mapper.ProdMapper;
import com.bwie.service.ProdService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
@Transactional
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod> implements ProdService{
    @Autowired
    private ProdMapper prodMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ProdInfoMapper prodInfoMapper;
    @Override
    public R getProdList(PageInfoVo pageInfoVo) {
        Page<Prod> prodPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<Prod> prodPage1 = prodMapper.selectPageList(prodPage,pageInfoVo);
        return R.OK(prodPage1);
    }

    @Override
    public R deleteProdBatch(List<Integer> ids) {
        prodMapper.deleteBatchIds(ids);
        prodMapper.delete(
                new QueryWrapper<Prod>().lambda().eq(Prod::getProdId,ids)
        );
        return R.OK();
    }

    @Override
    public R addProdList(Prod prod) {
        Prod prod1 = prodMapper.selectOne(
                new QueryWrapper<Prod>().lambda().eq(Prod::getProdSpu, prod.getProdSpu())
        );
        if(ObjectUtils.isNotEmpty(prod1)){
            return R.ERROR(500,"SUP名称重复，请重新输入");
        }
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return R.ERROR(500,"请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        prod.setCreateBy(userName);
        prod.setCreateTime(new Date());
        prod.setUpdateBy(userName);
        prod.setUpdateTime(new Date());
        prod.setProdStatus(0);
        prodMapper.insert(prod);
        return R.OK();
    }

    @Override
    public R selectProdDetail(Integer prodId) {
        Prod prod = prodMapper.selectProdById(prodId);
        if (ObjectUtils.isEmpty(prod)) {
            return R.ERROR(500,"该商品已存在");
        }
        return R.OK(prod);
    }

    @Override
    public R selectProdInfo() {
        List<ProdInfo> prodInfos = prodInfoMapper.selectList(null);
        return R.OK(prodInfos);
    }

    @Override
    public R addProdInfo(ProdInfo prodInfo) {
        String prodName = prodInfo.getProdName();
        Integer prodId = prodInfo.getProdId();
        ProdInfo one = prodInfoMapper.selectOne(new LambdaQueryWrapper<ProdInfo>()
                .eq(ProdInfo::getProdName, prodName)
                .eq(ProdInfo::getProdId, prodId)
        );
        if (ObjectUtils.isNotEmpty(one)){
            return R.ERROR(500,"该商品SKU已存在");
        }
        //添加商品
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return R.ERROR(500,"请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        prodInfo.setCreateBy(userName);
        prodInfo.setCreateTime(new Date());
        prodInfoMapper.insert(prodInfo);
        return R.OK();
    }

    @Override
    public R selectProdInfoByProdId(Integer prodId, PageInfoVo pageInfoVo) {
        Page<ProdInfo> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<ProdInfo> prodInfoPage = prodInfoMapper.selectPage(page,
                new LambdaQueryWrapper<ProdInfo>().eq(ProdInfo::getProdId, prodId));
        return R.OK(prodInfoPage);
    }
}
